from backend.utils import Interp


Table13 = (
(210,   150,    1.0,  2.0,  1.3,  2.6,  2.0,  4.0,  3.2,  6.4),
(420,   300,    2.0,  4.0,  2.0,  4.0,  2.0,  4.0,  3.2,  6.4),
(840,   600,    3.2,  6.4,  3.2,  6.4,  3.2,  6.4,  3.2,  6.4),
(1400,  1000,   4.2,  6.4,  4.2,  6.4,  4.2,  6.4,  4.2,  6.4),
(2800,  2000,   8.4,  8.4,  8.4,  8.4,  8.4,  8.4,  8.4,  8.4),
(7000,  5000,   17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5),
(9800,  7000,   25,   25,   25,   25,   25,   25,   25,   25),
(14000, 10000,  37,   37,   37,   37,   37,   37,   37,   37),
(28000, 20000,  80,   80,   80,   80,   80,   80,   80,   80)
)

def Table13_Process(pollution_class, 
              multiple_num, 
              voltage_type, 
              peak_voltage, 
              supply_voltage, 
              ):
    
    if voltage_type == 0:   # DC
        Vidx = 0
    else:   # AC
        Vidx = 1
    
    if supply_voltage <= 150:
        if pollution_class <= 1:
            ECidx = 2
        else:
            ECidx = 4
    elif supply_voltage <= 300:
        ECidx = 6
    else:
        ECidx = 8

    if multiple_num == 1:
        ECidx += 1

    if peak_voltage < Table13[0][Vidx]:
        ElecClearance = Table13[0][ECidx]
    for i in range(1, len(Table13)):
        if peak_voltage > Table13[i-1][Vidx] and peak_voltage <= Table13[i][Vidx]:
            if peak_voltage > 2800:
                ElecClearance = Table13[i][ECidx]
            else:
                ElecClearance = Interp(Table13[i-1][Vidx],  Table13[i][Vidx],
                                       Table13[i-1][ECidx], Table13[i][ECidx],
                                       peak_voltage)

    return ElecClearance
